package com.zr.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.crypto.provider.RSACipher;
import com.zr.dao.CommentDao;
import com.zr.entity.Comment;
import com.zr.entity.MainPost;
import com.zr.entity.Relation;
import com.zr.entity.User;
import com.zr.utils.DBUtils;
import com.zr.utils.UpdateDBUtils;

public class CommentDaoImpl implements CommentDao{

	@Override
	public boolean addComment(int uid,String text,int mid) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(text);
		String sql="insert into comment values(null,?,now(),?,?,?,?,null)";
		Object[] objs= {text,uid,0,0,mid};
		int res = DBUtils.modify(sql, objs);
		return res>0?true:false;
	}

	@Override
	public List<MainPost> showHot() throws SQLException {
		String sql="select * from mainpost ORDER BY mcount desc limit 0,5";
		ResultSet rs = DBUtils.query(sql);
		List<MainPost> mList=new ArrayList<MainPost>();
		while(rs.next()) {
			MainPost m = new MainPost(rs.getInt("mid"), rs.getString("mtitle"), rs.getString("mcontent"), null, rs.getString("mdate"), rs.getString("uid"), rs.getString("muname"),rs.getInt("mcount"), rs.getInt("bid"), null);
			mList.add(m);
		}
		return mList;
	}

	@Override
	public boolean upDownComment(int cid,int num) throws SQLException {
		String sql="update comment set ccount=count+? where cid=?";
		Object[] objs={num,cid};
		int res = DBUtils.modify(sql, objs);
		return res>0?true:false;
	}

	@Override
	public boolean toFriends(int uid,int fid) throws Exception {
		
		String sql="insert into relation values(null,?,?,now())";
		Object[] objs={uid,fid};
		System.out.println(uid+"--"+fid);
		int res = DBUtils.modify(sql, objs);
		System.out.println("res"+res);
		return res>0?true:false;
	}


	@Override
	public boolean deleteComment(int cid) throws Exception {
		String sql="delete from comment where cid=?";
		Object[] objs={cid};
		int res = DBUtils.modify(sql, objs);
		return res>0?true:false;
	}

	@Override
	public boolean updateComment(int cid, String ccontent) throws Exception {
		String sql="update comment set ccontent=? where cid=?";
		Object[] objs={cid,ccontent};
		int res = DBUtils.modify(sql, objs);
		return res>0?true:false;
	}

	@Override
	public List<Comment> getCommentById(int mid) throws Exception {
		String sql="select c.*,u.nickname from comment c inner join user u on c.uid=u.uid where mid=?";
		Object[] obj={mid};
		ResultSet rs = UpdateDBUtils.query(sql, obj);
		List<Comment> clist=new ArrayList<>();
		while (rs.next()) {
			Comment c = new Comment(rs.getInt("cid"),rs.getString("ccontent"),rs.getString("cdate"),rs.getInt("uid"),rs.getInt("ccount"),rs.getInt("cfloor"),rs.getInt("mid"),rs.getString("nickname"));
			clist.add(c);
			
		}
//		System.out.println(clList);
		return clist;
	}

	@Override
	public List<Comment> getAllComment() throws Exception {
		String sql="select * from comment";
		ResultSet rs = DBUtils.query(sql);
		List<Comment> clList=new ArrayList<>();
		while (rs.next()) {
			Comment c = new Comment(rs.getInt("cid"),rs.getString("ccontent"),rs.getString("cdate"),rs.getInt("uid"),rs.getInt("ccount"),rs.getInt("cfloor"),rs.getInt("mid"),rs.getString("cuname"));
			clList.add(c);
		}
		return clList;
	}

	@Override
	public boolean selectFriendById(int uid, int fid) throws SQLException {
		String sql="select * from relation where uid=? and fid=?";
		Object[] objs= {uid,fid};
		
		ResultSet rs = DBUtils.query(sql, objs);
		if (rs.next()) {
			return true;
		}
		return false;
	}

	public int updateUpDown(int mid, int updown) throws SQLException {
		String sql="update mainpost set mcount=? where mid=?";
		Object[] objs= {updown,mid};
		int res = DBUtils.modify(sql, objs);
		
		return res;
	}
	
	

}
